package com.shanghaionstar.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.onstar.cn.common.util.PayMeConstants;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.payme.payutil.wxpay.WXPayUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * 微信支付回调类
 * 
 * @author 7828wc
 *
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

	private IWXAPI api;

	public static final int WX_PAY_RESULT_CODE = 11000;

	@Override
	public BasePresenter getPresenter() {
		return null;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		api = WXAPIFactory.createWXAPI(this, PayMeConstants.APP_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(final BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Log.e("onResp","errcode == "+resp.errCode);
			WXPayUtil.WX_RESP_CODE = resp.errCode;
			RxBus.getDefault().post(WX_PAY_RESULT_CODE,resp);
			finish();

//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			String errorMsg = "";
//			// if (Constants.SIT_OR_UAT != 2) {
//			// errorMsg =
//			// getString(AlipayResult.getInstance().findResultByAlipayStatus(String.valueOf(resp.errCode)))
//			// + "(微信错误信息：" + resp.errStr + ")";
//			// } else {
////			errorMsg = getString(AlipayResult.getInstance().findResultByAlipayStatus(String.valueOf(resp.errCode)));
//			// }
//			builder.setMessage(errorMsg);
//			LogManager.getInstance().d("resp.errStr:" + resp.errStr);
//			builder.setPositiveButton(R.string.submit, new OnClickListener() {
//				@Override
//				public void onClick(DialogInterface dialog, int which) {
//					// Intent intent = new Intent(WXPayEntryActivity.this,
//					// PayMeOrderDetailActivity.class);
//					// intent.putExtra("returnCode",
//					// String.valueOf(resp.errCode));
//					// setResult(RESULT_OK, intent);
////					PayMeConstants.WECHAT_RESP_CODE = resp.errCode;
//					finish();
//				}
//			});
//			builder.show();
//			PayConstants.WECHAT_RESP_CODE = resp.errCode;
//			finish();
		}
	}
}